package com.jimmyhsu.ecnudaowei.coursetable;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.jimmyhsu.ecnudaowei.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jimmyhsu on 2017/3/25.
 */

public class ChooseWeekActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    public static final String BUNDLE_KEY_SELECTED_LIST = "bundle_key_selected_list";

    public static final String EXTRA_DATA = "extra_data";
    private String[] mDatas = new String[]{};
    private ArrayList<Integer> mSelectedPos = new ArrayList<>();

    @BindView(R.id.id_cw_lv)
    ListView mListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_week);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent != null) {
            mDatas = intent.getStringArrayExtra(EXTRA_DATA);
        }
        mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                mDatas) {
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                if (mSelectedPos.contains(position)) {
                    v.setBackgroundColor(ContextCompat.getColor(ChooseWeekActivity.this, R.color.colorSelected));
                } else {
                    v.setBackgroundColor(0x00000000);
                }
                return v;
            }
        });
        mListView.setOnItemClickListener(this);
    }

    public static void startActivityForResult(int requestCode, Context context, String[] data) {
        Intent intent = new Intent(context, ChooseWeekActivity.class);
        intent.putExtra(EXTRA_DATA, data);
        ((Activity)context).startActivityForResult(intent, requestCode);
        ((Activity)context).overridePendingTransition(R.anim.slide_from_right,
                R.anim.slide_to_left);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (!mSelectedPos.contains(position)) {
            mSelectedPos.add(position);
            view.setBackgroundColor(ContextCompat.getColor(this, R.color.colorSelected));
        } else {
            mSelectedPos.remove(Integer.valueOf(position));
            view.setBackgroundColor(0x00000000);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add(0, 0, 0, "完成");
        menuItem.setIcon(R.drawable.ic_check_white_24dp);
        menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == 0) {
            Bundle bundle = new Bundle();
            bundle.putIntegerArrayList(BUNDLE_KEY_SELECTED_LIST, mSelectedPos);
            Intent intent = new Intent();
            intent.putExtra(EXTRA_DATA, bundle);
            setResult(RESULT_OK, intent);
            finish();
            overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_rightt);
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        finish();
        overridePendingTransition(R.anim.slide_from_left, R.anim.slide_to_rightt);
    }
}